/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.flow.config;

import liquibase.integration.spring.SpringLiquibase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;

import javax.sql.DataSource;


@Configuration
public class DatabaseConfiguration {

    private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseConfiguration.class);

    protected static final String LIQUIBASE_CHANGELOG_PREFIX = "ACT_DE_";

//    @Bean
//    public Liquibase liquibase(DataSource dataSource) {
//        LOGGER.info("Configuring Liquibase");
//
//        Liquibase liquibase = null;
//        try {
//            DatabaseConnection connection = new JdbcConnection(dataSource.getConnection());
//            Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
//            database.setDatabaseChangeLogTableName(LIQUIBASE_CHANGELOG_PREFIX + database.getDatabaseChangeLogTableName());
//            database.setDatabaseChangeLogLockTableName(LIQUIBASE_CHANGELOG_PREFIX + database.getDatabaseChangeLogLockTableName());
//
//            liquibase = new Liquibase("META-INF/liquibase/flowable-modeler-app-db-changelog.xml", new ClassLoaderResourceAccessor(), database);
//            liquibase.update("flowable");
//            return liquibase;
//        } catch (Exception e) {
//            throw new InternalServerErrorException("Error creating liquibase database", e);
//        } finally {
//            closeDatabase(liquibase);
//        }
//    }
//
//    private void closeDatabase(Liquibase liquibase) {
//        if (liquibase != null) {
//            Database database = liquibase.getDatabase();
//            if (database != null) {
//                try {
//                    database.close();
//                } catch (DatabaseException e) {
//                    LOGGER.warn("Error closing database", e);
//                }
//            }
//        }
//    }

    @Bean
    public SpringLiquibase userLiquibase(DataSource dataSource) {
        SpringLiquibase liquibase = new SpringLiquibase();
        // 用户模块Liquibase文件路径
        liquibase.setChangeLog("classpath:liquibase/flowable-modeler-app-db-changelog.xml");
        liquibase.setDataSource(dataSource);
        liquibase.setShouldRun(false);
        liquibase.setResourceLoader(new DefaultResourceLoader());
        // 覆盖Liquibase changelog表名

//        liquibase.setDatabaseChangeLogTable("user_changelog_table");
//        liquibase.setDatabaseChangeLogLockTable("user_changelog_lock_table");
        return liquibase;
    }


}
